﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;

using ConstructionBidOnline.Biz;

public partial class admin_changepassword : System.Web.UI.Page
{
    
    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("sessionexpire.aspx");
        }
        
    }

    #endregion 

    #region Save Button Click 

    protected void btnCPSubmit_Click(object sender, ImageClickEventArgs e)
    {
        RegisteredUser ruObj = RegisteredUser.GetUserByUserName(Session["username"].ToString());
        if (ruObj == null)
        {
            Response.Redirect("sessionexpire.aspx");
        }
        else
        {
            ruObj.IsPasswordChangeRequired = false;
            if (ruObj.Password != RegisteredUser.HashPassword(txtCPCurrentPwd.Text.Trim()))
            {
                lblerror.Visible = true;
                lblerror.Text = "You have entered the wrong password. Please enter the correct password.";
                txtCPCurrentPwd.Focus();
                return;
            }
            if (txtCPNewPwd.Text.Length < 6)
            {
                lblerror.Visible = true;
                lblerror.Text = "Password lenght be of minimum six character long. Please enter the correct password.";
                txtCPNewPwd.Focus();
                return;
            }
            if (txtCPNewPwd.Text.Trim() != txtCPConfirmNewPwd.Text.Trim())
            {
                lblerror.Visible = true;
                lblerror.Text = "Confirm Password does not matches with the new password . Please enter the correct password.";
                txtCPConfirmNewPwd.Focus();
                return;
            }
            ruObj.Password = RegisteredUser.HashPassword(txtCPNewPwd.Text.Trim());
            ruObj.IsPasswordChangeRequired = false;
            RegisteredUser.Update(ruObj);
            lblerror.Visible = true;
            divforbuttons.Visible = false;
            lblerror.Text = "Your password has been changed successfully. <br/> You are requested to re-login, just for some security reasons, You will be automatically redirect to login page within 5 seconds..";
            Session.Clear();
            Session.RemoveAll();
            String strscript = "<script language=javascript>refreshandclose();</script>";
            if (!Page.IsStartupScriptRegistered("clientScript"))
                Page.RegisterStartupScript("clientScript", strscript);           
        }
    }

    #endregion 

    #region Cancel Button Click

    protected void btnCPCancel_Click(object sender, ImageClickEventArgs e)
    {
        RegisteredUser ruObj = RegisteredUser.GetUserByUserName(Session["username"].ToString());

        if (ruObj == null)
        {
            Response.Redirect("sessionexpire.aspx");
        }
        else
        {
            ruObj.IsPasswordChangeRequired = false;
            RegisteredUser.Update(ruObj);
            lblerror.Visible = true;
            lblerror.Text = "Your current password will be remain intact, and the same will be treated as your Login ID's Password for the later use. <br/> <br/> <a href='javascript:window.close();'>Click here</a> to continue...";
            divforbuttons.Visible = false;
        }
    }
    
    #endregion

    #endregion

}
